import { Modal } from 'antd';
import {WeaTools} from "ecCom";
const getKey = WeaTools.getKey;
import isEmpty from 'lodash/isEmpty';
import isArray from 'lodash/isArray';

export const transfStr = (name = '', ids = '', list = [], type = '') => {
	let str = '';
	const mirror = {
		37: "doc",
		prjtsk: "task",
		18: "crm",
		152: "workflow",
		135: "project",
	}
	list.map(item => {
		if(name === 'Upload' && type === 'image'){
			str += '<img class="formImgPlay" src="' + item.filelink + '" data-imgsrc="' + (item.loadlink || item.filelink) + '" />'
		}
		if(name === 'Upload' && type === 'file'){
			str += (`<a href='javascript:void(0)'  style='cursor:pointer;text-decoration:underline !important;margin-right:8px'>${item.filename}</a><br>`)
		}
		if(name === 'Browser'){
			str += ( `<a href='javascript:void(0)'  style='cursor:pointer;text-decoration:underline !important;margin-right:8px'>${item.name || item.showname}</a><br>`)
		}
	})
	return str
}

export const  datediff=(sDate1,sDate2,sDate3,sDate4,manager,passnoworktime)=>{
		  var aDate, oDate1, oDate2, iDays,odate3,odate4,tdate,otime1,otime2,oDatetime1, oDatetime2;
		  
		  if(passnoworktime!=1){
		  
			oDate1=new Date(sDate1);
			oDate2=new Date(sDate2);
			odate3=new Date(sDate3);
			odate4=new Date(sDate4);
			
			if(true || ""+oDate1.getFullYear()=="NaN"){
				aDate = sDate1.split("-")
				tdate = sDate3.split(":")
				oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])   //转换为12-18-2002格式
				oDatetime1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]+" "+tdate[0]+":"+tdate[1]) 
				//if(window.console) console.log("oDate1:"+oDate1);
				//if(window.console) console.log("oDatetime1:"+oDatetime1);
				aDate = sDate2.split("-")
				tdate = sDate4.split(":")
				oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
				oDatetime2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]+" "+tdate[0]+":"+tdate[1])
			
			}
			//if(window.console) console.log("oDatetime1:"+(oDatetime1));
			//if(window.console) console.log("oDatetime2:"+(oDatetime2));
			
			if(oDate2-oDate1<0){
					iDays = -1 	//日期
			}else if(oDate2-oDate1==0){
				if(sDate4<sDate3){
					iDays = -2 //时间
				}else{
					iDays = (Math.abs(oDatetime1-oDatetime2) / 1000 / 60 / 60 /24).toFixed(2)     
				}
			}else{
				 iDays = (Math.abs(oDatetime1-oDatetime2) / 1000 / 60 / 60 /24).toFixed(2)     
			}
			if(window.console) console.log("iDays:"+(iDays));
		  }else{   //计算去掉不包含非工作日
			  
		   $.ajax({
			   type: "post",
			   url: "/proj/process/GetWorkDays.jsp",
			   data:"begindate="+sDate1+"&begintime="+sDate3+"&enddate="+sDate2+"&endtime="+sDate4+"&manager="+manager,
			   dataType: "text", 
			   async:false,
			   success:function(data){
					iDays = data.trim();   
			   }
		   });
		  }
		
		return iDays;

	}

export const openWindow = url => {
	const width = screen.availWidth - 10;
	const height = screen.availHeight - 50;
	let szFeatures = 'top=0,';
	szFeatures += 'left=0,';
	szFeatures += `width=${width},`;
	szFeatures += `height=${height},`;
	szFeatures += 'directories=no,';
	szFeatures += 'status=yes,toolbar=no,location=no,';
	szFeatures += 'menubar=no,';
	szFeatures += 'scrollbars=yes,';
	szFeatures += 'resizable=yes';
	window.open(url, '', szFeatures);
};
	
window.openFullWindowHaveBar = openWindow;
window.openFullWindowForXtable = openWindow;

window.cptOpenPage = function(routeUrl){
    window.open("/spa/cpt/index.html#"+routeUrl, "_blank");
}

export const getFormInitDatas = (condition=[]) =>{
	let initdatas  = {};
	condition.map(c =>{
		let items = [];
		c.items.map((field,index) => {
			const key = getKey(field);
			if(field.conditionType == "BROWSER"){
				if (!isEmpty(field.browserConditionParam)) {
					const replacesDatas = field.browserConditionParam.replaceDatas || field.browserConditionParam.valueObj;
					if (!isEmpty(replacesDatas)) {
					  let values = [], names = [];
					  replacesDatas.forEach((d)=> {
						values.push(d.id);
						names.push(d.lastname || d.name); // 人力按钮特殊处理
					  });
					  initdatas[key] =  {
						value: values.join(','),
						valueSpan: names.join(','),
						valueObj: replacesDatas
					  }
					}
				}
			}else if(field.conditionType == 'DATE'){
				let selectValue = '0', startValue = null, endValue = null;
				if (!isEmpty(field.options)) selectValue = WeaTools.getSelectDefaultValue(field.options) || selectValue;
				if (!isEmpty(field.value)) {
				  selectValue = field.value[field.domkey[0]];
				  startValue = field.value[field.domkey[1]];
				  endValue = field.value[field.domkey[2]];
				}
				initdatas[key] ={
				  value: [selectValue, startValue, endValue]
				}
			}else if(field.conditionType == 'SELECT'){
				let value = WeaTools.getSelectDefaultValue(field.options);
				if (field.value !== undefined) value = field.value;
				if (value !== undefined) {
					initdatas[key] = {
					value: value
				  }
				}
			}else if(field.conditionType == 'SELECT_LINKAGE'){
				let selectValue = WeaTools.getSelectDefaultValue(field.options) || '0';
				initdatas[key] = {
				  value: [selectValue]
				}
			}else if(field.conditionType == "SCOPE"){
				let sValue = field.startValue || '', eValue = field.endValue || '';
				initdatas[key] =  {
				  value: [sValue, eValue]
				}
			}else if(field.conditionType ==  'RANGEPICKER'){
				let sValue = field.startDate || field[field.domkey[0]] || '', eValue = field.endDate || field[field.domkey[1]] || '';
				initdatas[key] = {
				  value: [sValue, eValue]
				}
			}else{

				initdatas[key] = {value:field.value || ""};
			}
			
		});
	  });
	  return initdatas;
}